package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.commons.LogOperation;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> page(StudentQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Student> StuList = studentMapper.page(param);
        Page<Student> p = (Page<Student>) StuList;
        return new PageResult<Student>(p.getTotal(), p.getResult());
    }

    @LogOperation
    @Override
    public void delete(Integer[] ids) {
        studentMapper.delete(ids);
    }

    @LogOperation
    @Override
    public void insert(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    @LogOperation
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);

    }

    @Override
    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }

    @LogOperation
    @Override
    public void violationUpdate(Integer id, Integer score) {
        Student student = studentMapper.selectById(id);
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationScore((short) (student.getViolationScore() + score));
        student.setViolationCount((short) (student.getViolationCount() + 1));
        studentMapper.update(student);

    }
}
